//
//  HRZGlobalNetManager.m
//  EMarket
//
//  Created by 物联速通信息技术有限公司 on 2018/5/23.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZGlobalNetManager.h"

// Controllers

// Models
// Views

// Vendors

// Categories

// Others
#import "HRZTool.h"
// Service
#import "HRZAuthorService.h"
#import "HRZGoodsService.h"
#import "HRZCartService.h"
#import "HRZHomeAdService.h"
#import "HRZOrderService.h"

@interface HRZGlobalNetManager ()
{
    HRZSearchThemeGoodsModel *_themGoodsModel;
    HRZGoodsClassifyListSearchModel *_searchGoodsListModel;
}

@end

@implementation HRZGlobalNetManager

+ (instancetype)getGlobalNetManager {
    static HRZGlobalNetManager *_mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _mgr = [[HRZGlobalNetManager alloc] init];
    });
    return _mgr;
}

@end

#pragma mark - ----------  Home  ----------

#import "HRZSearchThemeGoodsModel.h"
#import "HRZGoodsClassifyListSearchModel.h"
#import "HRZHomeItemBaseModel.h"

@implementation HRZGlobalNetManager (Home)

- (void)homePageInfoComplete:(HomeInfoData)complete {
    
    __block BOOL isFail;
    __block HRZHomeItemBaseModel *baseModel = [HRZHomeItemBaseModel new];
    
    dispatch_group_t downloadGroup = dispatch_group_create();
    
    /** 轮播图 */
    dispatch_group_enter(downloadGroup);
    [HRZHomeAdService getHomeCarouselSuccess:^(NSArray<HRZHomeCarouselModel *> *arr) {

        NSMutableArray *mArray = [NSMutableArray array];
        for (HRZHomeCarouselModel *model in arr) {
            [mArray addObject:[HRZTool homeRecommentNetItemTurnIOSItem:model]];
        }
        HRZHomeItemModel *model = [HRZHomeItemModel new];
        model.type = HomeItemTypeCarousel;
        model.items = mArray;
        [baseModel.itemModels addObject:model];
        
        dispatch_group_leave(downloadGroup);
    } failure:^(NSString *err) {
        isFail = YES;
        dispatch_group_leave(downloadGroup);

    }];
    
    /** 热门分类 */
    dispatch_group_enter(downloadGroup);
    [HRZHomeAdService getHotClassifySuccess:^(NSArray<HRZHomeHotClassifyModel *> *arr) {
        
        NSMutableArray *mArray = [NSMutableArray array];
        for (HRZHomeHotClassifyModel *model in arr) {
            [mArray addObject:[HRZTool homeRecommentNetItemTurnIOSItem:model]];
        }
        
        HRZHomeItemModel *model = [HRZHomeItemModel new];
        model.type = HomeItemTypeHotClassify;
        model.items = mArray;
        [baseModel.itemModels addObject:model];
        
        dispatch_group_leave(downloadGroup);

    } failure:^(NSString *err) {
        isFail = YES;
        dispatch_group_leave(downloadGroup);
    }];
    

    /** 主题 */
    dispatch_group_enter(downloadGroup);
    [HRZHomeAdService getThemeSuccess:^(NSArray<HRZHomeThemeModel *> *arr) {
        
        NSMutableArray *mArray = [NSMutableArray array];
        for (HRZHomeThemeModel *model in arr) {
            [mArray addObject:[HRZTool homeRecommentNetItemTurnIOSItem:model]];
        }
        
        HRZHomeItemModel *model = [HRZHomeItemModel new];
        model.type = HomeItemTypeTheme;
        model.items = mArray;
        [baseModel.itemModels addObject:model];
        dispatch_group_leave(downloadGroup);
    } failure:^(NSString *err) {
        isFail = YES;
        dispatch_group_leave(downloadGroup);
    }];
    
    /** 大牌 */
    dispatch_group_enter(downloadGroup);
    [HRZHomeAdService getBigbrandsSuccess:^(NSArray<HRZHomeBigbrandModel *> *arr) {
       
        NSMutableArray *mArray = [NSMutableArray array];
        for (HRZHomeBigbrandModel *model in arr) {
            [mArray addObject:[HRZTool homeRecommentNetItemTurnIOSItem:model]];
        }
        
        HRZHomeItemModel *model = [HRZHomeItemModel new];
        model.type = HomeItemTypeBigbrand;
        model.items = mArray;
        [baseModel.itemModels addObject:model];
        
        dispatch_group_leave(downloadGroup);
    } failure:^(NSString *err) {
        isFail = YES;
        dispatch_group_leave(downloadGroup);
    }];
    
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
        complete(baseModel, !isFail);
    });
    
}


- (void)homeSearchThemeGoodsByThemeModel:(HRZSearchThemeGoodsModel *)model complete:(HomeThemeGoodsData)complete{
    
    _themGoodsModel = model;

    [HRZHomeAdService getThemeGoodsByTheme:_themGoodsModel success:^(NSArray<HRZGoodsClassifyDetailModel *> *arr) {
        complete(arr, YES, NO);
        
    } failure:^(NSString *err) {
        
        complete(NULL, NO, NO);
        
    }];
}

- (void)homeSearchThemeGoodsMoreComplete:(HomeThemeGoodsData)complete {
    _themGoodsModel.page += 1;
    [HRZHomeAdService getThemeGoodsByTheme:_themGoodsModel success:^(NSArray<HRZGoodsClassifyDetailModel *> *arr) {
        
        complete(arr, YES, YES);
        
    } failure:^(NSString *err) {
        
        complete(NULL, NO, YES);
        
    }];
}

@end

#pragma mark - ----------  Goods  ----------
@implementation HRZGlobalNetManager (Goods)

- (void)onSearchGoodsClassifyComplete:(GoodsFirstClassifyResult)complete {
    
    [HRZGoodsService getGoodsBaseClassifySuccess:^(NSArray<HRZGoodsClassifyBaseModel *> *classfyArray) {
        complete(classfyArray, YES);
    } failure:^(NSString *error) {
        complete(NULL, NO);
    }];
    
}

- (void)onSearchGoodsClassifyContentByClassifyId:(NSString *)classifyId complete:(GoodsSecondClassifyResult)complete{
    [HRZGoodsService getGoodsClassifyContentByClassifyId:classifyId success:^(HRZGoodsClassifyContentModel *classfyModel) {
        complete(classfyModel, YES);
        
    } failure:^(NSString *error) {
        complete(NULL, YES);
    }];
}

- (void)onSearchGoodsListBySerachModel:(HRZGoodsClassifyListSearchModel *)model complete:(GoodsClassifyListResult)complete{
    _searchGoodsListModel = model;
    
    [HRZGoodsService getGoodsListByClassifyModel:_searchGoodsListModel success:^(NSArray<HRZGoodsClassifyDetailModel *> *goodsArray) {
        
        complete(goodsArray, YES, NO);
        
    } failure:^(NSString *error) {
        
        complete(NULL, NO, NO);
    }];
}

- (void)onSearchMoreGoodsListComplete:(GoodsClassifyListResult)complete {
    
    _searchGoodsListModel.page += 1;
    
    [HRZGoodsService getGoodsListByClassifyModel:_searchGoodsListModel success:^(NSArray<HRZGoodsClassifyDetailModel *> *goodsArray) {
        
        complete(goodsArray, YES, YES);
        
    } failure:^(NSString *error) {
        
        complete(NULL, NO, YES);
        
    }];
}

- (void)onSearchGoodsDetailByGoodsId:(NSString *)goodsId complete:(GoodsDetailResul)complete{
    [HRZGoodsService getGoodsDetailByGoodsId:goodsId success:^(HRZGoodsDetailModel *goodsModel) {
        
        complete(goodsModel, YES);
        
    } failure:^(NSString *error) {
        
        complete(NULL, NO);
        
    }];
}

@end

#pragma mark - ----------  Cart  ----------
#import "HRZGetShopCarListModel.h"
#import "HRZAddShopCartModel.h"
#import "HRZDeleteCarModel.h"

@implementation HRZGlobalNetManager (Cart)

- (void)addCartGoodsWithGoodsId:(NSString *)goodsId
                       complete:(OperationResult)complete {
    HRZAddShopCartModel *model = [[HRZAddShopCartModel alloc] initWithGoodsId:goodsId];
    
    [HRZCartService addGoodsToCartWith:model success:^(NSString *suc) {
        complete(YES);
    } failure:^(NSString *error) {
        complete(NO);
    }];
}

- (void)updateCartGoodsNumberByCartModel:(HRZUpdateNumberModel *)model complete:(OperationResult)complete{
    [HRZCartService updateCartGoodsNunberWith:model success:^(NSString *success) {
        
        complete(YES);
        
    } failure:^(NSString *error) {
        
        complete(NO);
        
    }];
}

- (void)deletedCartGoodsId:(NSString *)goodsId complete:(OperationResult)complete {
    
    HRZDeleteCarModel *model = [[HRZDeleteCarModel alloc] initWithGoodsId:goodsId];
    
    [HRZCartService deletedGoodsFromCart:model success:^(NSString *success) {
        
        complete(YES);
        
    } failure:^(NSString *error) {
        
        complete(NO);
        
    }];
}

- (void)onSearchCartGoodsListByCurrentShopComplete:(CartGetList)complete {
    HRZGetShopCarListModel *model = [[HRZGetShopCarListModel alloc] initCurrentModel];
    
    [HRZCartService getAllCartGoodsWithCurrentUserId:model success:^(NSArray *goodses) {
        
        complete(goodses, YES);
        
    } failure:^(NSString *error) {
        
        complete(@[], NO);
        
    }];
}

@end

#pragma mark - ----------  User  ----------
@implementation HRZGlobalNetManager (User)

- (void)onUserRegisterByPhone:(NSString *)phone phoneCode:(NSString *)phoneCode passWord:(NSString *)passWord complete:(UserRegisterData)complete{
    
    [HRZAuthorService userRegisterWithPhoneNum:phone verityNum:phoneCode password:passWord success:^(NSString *suc) {
        
        complete(NULL, YES);
        
    } failure:^(NSString *err) {
        
        complete(NULL, NO);
        
    }];
    
}

- (void)onUserLoginByPhone:(NSString *)phone passWord:(NSString *)passWord phoneIdentity:(NSString *)phoneId complete:(UserLoginData)complete{
    
    [HRZAuthorService userLoginWithUserAccount:phone password:passWord phoneId:phoneId success:^(HRZUserInfo *userInfo) {
        
        [HRZTool setUserLoginStatus:YES];
        [HRZTool setUserShopInfo:userInfo];
        
        complete(userInfo, YES);
        
    } failure:^(NSString *err) {
        
        [HRZTool setUserLoginStatus:NO];
        [HRZTool setUserShopInfo:NULL];
        
        complete(NULL, NO);
        
    }];
    
}

- (void)onUserShopAttestationWith:(HRZAttestationModel *)model complete:(AttestationStoreData)complete{
    [HRZAuthorService userShopAttestationWith:model success:^(NSString *suc) {
        
        complete(suc, YES);
        
    } failure:^(NSString *err) {
        
        complete(@"", NO);
        
    }];
}

- (void)onUserShopAddressAddWith:(HRZAddressUpdateModel *)model complete:(OperationResult)complete{
    [HRZAuthorService userShopAddressWith:model success:^(NSString *suc) {
        
        complete(YES);
        
    } failure:^(NSString *err) {
       
        complete(NO);

    }];
}

- (void)onSearchShopAddressByShopId:(NSString *)shopId complete:(OperationResult)complete{
    [HRZAuthorService searchShopAddressByShopId:[shopId integerValue] success:^(NSString *suc) {
        
        complete(YES);

    } failure:^(NSString *err) {
       
        complete(NO);
        
    }];
}

- (void)onUserShopUploadImage:(UIImage *)image complete:(FileUploadData)complete{
    [HRZAuthorService userUpdloadImage:image success:^(NSString *suc) {
        
        complete(suc, YES);
        
    } failure:^(NSString *err) {
       
        complete(err, NO);
    }];
}

@end

#pragma mark - ----------  Order  ----------
#import "HRZOrderStatusModel.h"
#import "HRZSearchOrderDetailModel.h"
#import "HRZIssuedOrderModel.h"
#import "HRZOrderPayModel.h"
#import "HRZCartGoodsDetailModel.h"

@implementation HRZGlobalNetManager (Order)

- (void)onCreateOrderWithCartGoods:(NSArray<HRZCartGoodsDetailModel *> *)array
                          complete:(CreateOrderData)complete {
    
    NSMutableArray *mArr = [NSMutableArray array];
    
    for (HRZCartGoodsDetailModel *model in array) {
        NSString *modelId = model.goodsId;
        NSInteger modelNum = [model.goodsNumber integerValue];
        NSDictionary *dic = @{@"goodsId":modelId,@"goodsNumber":@(modelNum)};
        [mArr addObject:dic];
    }
    
    
    
    HRZOrderDetailDtos *dtos = [[HRZOrderDetailDtos alloc] init];
    dtos.orderDetailDtos = mArr;
    
    HRZIssuedOrderModel *model = [[HRZIssuedOrderModel alloc] initWithOrder:[dtos mj_JSONString]];
    
    [HRZOrderService creatOrderByOrderModel:model success:^(NSString *suc) {
        complete(suc,YES);
    } failure:^(NSString *err) {
        complete(err,NO);
    }];
    
}

- (void)onCreateOrderPayWithOrderId:(NSString *)orderId
                            payType:(HRZOrderPayType)payType
                           complete:(CreateOrderType)complete {
    
    HRZOrderPayModel *model = [[HRZOrderPayModel alloc] initWithOrderId:orderId payType:(OrderPayType)payType];
    
    [HRZOrderService createOrderPayModel:model success:^(NSString *suc) {
        complete(suc, YES);
    } failure:^(NSString *err) {
        complete(err, YES);
    }];
}

- (void)onSearchOrderByOrderStatus:(HRZOrderStatus)status complete:(SearchOrderSatus)complete {
    NSString *orderStatus = @"";
    switch (status) {
            case HRZOrderStatusUnPay:
            orderStatus = @"140001";
            break;
            case HRZOrderStatusUnSend:
            orderStatus = @"140002";
            break;
            case HRZOrderStatusSend:
            orderStatus = @"140003";
            break;
            case HRZOrderStatusUnComment:
            orderStatus = @"140004";
            break;
            case HRZOrderStatusComment:
            orderStatus = @"140005";
            break;
    }
    
    HRZOrderStatusModel *model = [[HRZOrderStatusModel alloc] initWithOrderStatus:orderStatus];
    
    [HRZOrderService searchOrderClassifyByOrderStatus:model success:^(NSArray<HRZOrderModel *> *array) {
        complete(array, YES);
    } failure:^(NSString *err) {
        complete(NULL, NO);
    }];
}

- (void)onSearchOrderDetailByOrderId:(NSString *)orderId complete:(SearchOrderDetail)complete {
    HRZSearchOrderDetailModel *model = [[HRZSearchOrderDetailModel alloc] init];
    model.orderId = orderId;
    
    [HRZOrderService searchOrderDetailByModel:model success:^(HRZOrderModel *model) {
        complete(model, YES);
    } failure:^(NSString *err) {
        complete(NULL, NO);
    }];
}

@end
